<?php

include_once('global.php');

//--------------------------------------------------------------------------------
// Display the page for a DOM document
//--------------------------------------------------------------------------------
function showDOM() {
  global $dom;
  global $debug;
  global $skin;
  $html = skin($dom, $skin);
  print $html;
  if( $debug ) {
    print "<p><font size='1'>original XML follows...";
    print htmlentities($dom->saveXML());
  }
}

//--------------------------------------------------------------------------------
// Create a DOM document for display
//--------------------------------------------------------------------------------
function newDOM() {
  global $xmlroot;
  global $sessionid;
  global $debug;
  global $pseudo;
  global $url;
  global $thumbsurl;
  global $imagesurl;
  global $back;
  $dom = new DomDocument('1.0', 'iso-8859-1');
  $xroot = $dom->createElement($xmlroot);
  $xroot->setAttribute("url", htmlentities($url));
  $xroot->setAttribute("thumbsurl", htmlentities($thumbsurl));
  $xroot->setAttribute("imagesurl", htmlentities($imagesurl));
  if( $debug )
    $xroot->setAttribute("debug", "1"); 
  $dom->appendChild($xroot);
  return $dom;
}

//--------------------------------------------------------------------------------
// Add a fault message
//--------------------------------------------------------------------------------
function addFault($message) {
  global $dom;
  $xroot = $dom->documentElement;
  $xfault = $dom->createElement("fault");
  $xfault->setAttribute("message", htmlentities($message));
  $xroot->appendChild($xfault);
  return true;
}

function addWarn($message) {
  global $dom;
  $xroot = $dom->documentElement;
  $xfault = $dom->createElement("warn");
  $xfault->setAttribute("message", htmlentities($message));
  $xroot->appendChild($xfault);
  return true;
}

//--------------------------------------------------------------------------------
// XSL transform using a stylesheet
// @param $xml DOM to transform
// @param $xslfile full qualified name of xsl file to use for transformation
// @return the transformed string 
//--------------------------------------------------------------------------------
function transform($xml, $xslfile) {
  $xsl = new DomDocument;
  $xsl->load($xslfile);

  $xslt = new XsltProcessor();
  $xslt->importStyleSheet($xsl);

  $processed = $xslt->transformToXML($xml);

  unset($xslt);
  return $processed;
}

//--------------------------------------------------------------------------------
// Skin an XML string
//--------------------------------------------------------------------------------
function skin($xmlstring, $xslfile) {
  global $homedir;
  $xslfile = "$homedir/$xslfile";
  $res = transform($xmlstring, $xslfile);
  return $res;
}

?>
